package org.third.spring.boot.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.http.MediaType;
//import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.third.spring.boot.hello.domain.HelloUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@ComponentScan(basePackages = { "org.third.spring.boot.hello" })
// @EnableAutoConfiguration
// @EnableRetry
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
		FlywayAutoConfiguration.class })
@EnableCaching
@EnableScheduling
//@EnableAutoConfiguration

//@EnableRetry
//@EnableAsync
@EnableJpaRepositories(basePackages = "org.third.spring.boot.hello.repository")
@EntityScan("org.third.spring.boot.hello")

@ServletComponentScan
@RequestMapping(value = "/api/v1")
public class HelloApplication {
	@RequestMapping("/")
	@ResponseBody
	String health() {
		return "I am fine";
	}

	@RequestMapping("/hello")
	@ResponseBody
	String home() {
		return "Hello ,spring boot!";
	}

	@RequestMapping(value = "/hello1", method = { RequestMethod.POST }, consumes = {
			MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE })
	String hello1(@RequestBody(required = false) String status, HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println(request.getParameterMap());
		return "Hello ,spring boot!" + status;
	}

	@RequestMapping(value = "/hello2", method = { RequestMethod.POST }, consumes = {
			MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE })
	String hello2(@RequestBody(required = false) String status, HttpServletRequest request,
			HttpServletResponse response) {
		return "Hello ,spring boot!" + status;
	}

	@RequestMapping(value = "/hello3", method = { RequestMethod.PUT }, consumes = {
			MediaType.APPLICATION_FORM_URLENCODED_VALUE })
	@ResponseBody
	String hello3(@RequestParam String status) {

		System.out.println("status=" + status);

		return "Hello ,spring boot!" + status;
	}

	@RequestMapping(value = "/hello3", method = { RequestMethod.PUT }, consumes = {
			MediaType.APPLICATION_JSON_UTF8_VALUE })
	@ResponseBody
	String hello3(@RequestParam String status, @RequestBody(required = false) HelloUser user) {

		return "Hello ,spring boot!" + user;
	}

	@RequestMapping(value = "/hello4", method = { RequestMethod.PUT }, consumes = {
			MediaType.APPLICATION_FORM_URLENCODED_VALUE })
	@ResponseBody
	String hello4(@RequestBody(required = false) String status, HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println(request.getParameterMap());
		return "Hello ,spring boot!" + status;
	}

	public static void main(String[] args) throws Exception {
		SpringApplication.run(HelloApplication.class, args);
		// d 运行之后在浏览器中访问：http://localhost:8080/hello
	}

}